#include <soar.h>

#define MODULE_NAME "REFRESH"

#define	AREA_FILE   "sample_shm_area"

struct rec
{
    int recno;
};
struct rec *rec;


static int proc(int id)
{
    int i;

    for(i=0; i<100; i++)
    {
        shm_area_enter(rec);

        rec->recno++;
        printf("id(%d): recno: %d\n", id, rec->recno);

        if(random_int(1, 5) == 1)
        {
            printf("id(%d): sleep <<<<<\n", id);
            usleep(500*1000);
            printf("id(%d): wakeup >>>>>\n", id);
        }

        shm_area_leave(rec);

        //if(random_int(1, 3) == 1)
        //{
        //    usleep(500*1000);
        //}
    }

    return(0);
}


int main(int argc, char *argv[])
{
    char buf[BUFFER_SIZE];
    int result;
    int id;

    if(argc != 2)
        id=0;
    else
        id=atoi(argv[1]);

    sprintf(buf, "%s.LOG", MODULE_NAME);
    //trace_stdout(buf);

    if((rec=shm_area_attach(AREA_FILE)) == NULL)
    {
        printf("%s: shm_area_attach fail, result: %d\n",
                __func__, result);
        return(-1);
    }

    proc(id);

    if((result=shm_area_detach((void *)rec)))
    {
        printf("%s: shm_area_detach fail, result: %d\n",
                __func__, result);
        return(-1);
    }

    return(0);
}


